if(!Function.prototype.bind){
	Function.prototype.bind=function(obj){
		var args=Array.prototype.slice.call(arguments,1);
		var selfs = this;
		return function(){
			selfs.apply(obj,args.concat(Array.prototype.slice.call(arguments)));
		}
	}
}

var inArray=function(arr){
	for(var i=0;i<arr.length;i++){
		if(this==arr[i]){
			return true
		}
	}
	return false
}
String.prototype.inArray = inArray
Number.prototype.inArray = inArray

window.onload=function(){
	// 拦截element-ui单个表单回车提交事件
	var s=document.getElementsByClassName('el-form')
	for(var i=0;i<s.length;i++){
		s[i].onsubmit=function(){
			return false
		}
	}
}
function Storage(){
	var modal={}
	modal.get=function(key){
		return JSON.parse(window.localStorage.getItem(key))
	}
	modal.set=function(key,value){
		window.localStorage.setItem(key,JSON.stringify(value))
	}
	return modal
}
function Http(){
	var modal={};
	modal.cookie=function(name){
		var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
		if(arr=document.cookie.match(reg)){
			return unescape(arr[2]);
		}else{
			return null;
		}
	}
	modal.isUse=function(){
		if (window.XMLHttpRequest){
			return true;
		}else{
			return false;
		}
	}
	modal.create=function(options){
		var request=new XMLHttpRequest();
		var method=options.method || 'GET'
		if(method=='PUT'){
			method='POST'
		}
		var url=(typeof options.url !== 'undefined') ?  options.url : window.location.href;
		if(options.params){
			var p=''
			for(var key in options.params){
				p += key + '=' + options.params[key] + '&'
			}
			options.params = p.substring(0,p.length-1)
			if(url=='' || url.indexOf('?')===-1){
				url+='?'+options.params
			}else{
				url+='&'+options.params
			}
		}
		request.open(method,url,true)
		request.timeout=options.timeout || 15000
		request.onreadystatechange=function(e){
			var response=e.target

			if(response.readyState===4){
				if(options.complete){
					options.complete(response)
				}
				if(response.status==200){
					options.success(JSON.parse(response.response))
				}else{
					if(options.fail){
						options.fail(response)
					}else{
						console.log(response)
					}
				}
			}else if(response.readyState===2){
				if(options.loadStart){
					options.loadStart(response)
				}
			}
		}
		request.setRequestHeader('X-Requested-With','XMLHttpRequest')
		if(options.method=='POST'){
			request.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
			request.setRequestHeader('X-XSRF-TOKEN',modal.cookie('XSRF-TOKEN'))
			request.send(JSON.stringify(options.data))
		}else if(options.method=='PUT'){
			request.setRequestHeader('X-XSRF-TOKEN',modal.cookie('XSRF-TOKEN'))
			request.send(options.data)
		}else{
			request.send()
		}

	}
	modal.get=function(options){
		options.success=options.success.bind(vm)
		modal.create(options)
	}
	modal.post=function(options){
		options.method='POST'
		options.success=options.success.bind(vm)
		modal.create(options)
	}
	modal.put=function(options){
		options.method='PUT'
		var fd=new FormData()
		fd.append('file',options.data)
		options.data=fd
		options.success=options.success.bind(vm)
		modal.create(options)
	}
	return modal;
}
var vm=null;
function Ke(){
	var modal={};
	modal.data={};
	modal.init=function(menu){
		modal.data=menu;
	}
	modal.create=function(options){
		options.el='#app'
		var vars={}

		// 全局方法
		var methods={
			// font awesome
			iconFormat:function(type){
				return 'fa fa-fw fa-'+type
			},
			url:function(url){
				top.vm.httpsrc = url
			},
			maps:function(url,params){
				var u = this.map[url]
				if(params){
					u += '?'
					for(var k in params){
						u += k + '=' + params[k] + '&'
					}
					u = u.substring(0,u.length-1)
				}

				if(top.vm){
					top.vm.httpsrc = u
				}else{
					this.httpsrc = u
				}

			},
			reload:function(){
				location.reload()
			},
			scrollTop:function(){
				document.body.scrollTop = 0
				document.documentElement.scrollTop = 0
			}
		}

		if(options.data){
			if(typeof ke.data != 'undefined'){
				vars=Object.assign(options.data,ke.data)
			}
		}else{
			vars=ke.data
		}
		vars.httpsrc=null
		options.data=function(){
			return vars
		}
		if(typeof options.methods != 'undefined'){
			options.methods=Object.assign(options.methods,methods)
		}else{
			options.methods=methods
		}


		// 
		// 
		// 
		// 自定义指令 start
		Vue.directive('focus',{
			update:function(el,options){
				if(options.value){
					var s=el.getElementsByTagName('input')
					for (var i = s.length - 1; i >= 0; i--) {
						s[i].focus()
					}
				}
			}
		})
		// 自定义指令 end
		// 
		// 
		// 
		vm=new Vue(options)
	}
	modal.submitForm=function(options){
		if(typeof options=='undefined'){
			options={url:''}
		}
		var load=vm.$loading({
			lock: true,
			text:'加载中',
			background: 'rgba(0, 0, 0, 0)'
		});

		http.post({
			url: options.url,
			data: options.data,
			complete:function(t){
				load.close();
				if(options.complete){
					options.complete(t);
				}
			},
			success:options.success
		})
	}
	return modal;
}

var storage=new Storage();
var http=new Http();
var ke=new Ke();